package com.lcy.testjta.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lcy.testjta.domain.car.User;
import com.lcy.testjta.domain.test.Role;
import com.lcy.testjta.mapper.car.UserMapper;
import com.lcy.testjta.mapper.test.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Random;

/**
 * @author player six
 * @email jslcylcy@163.com
 * @create 2018-03-29 11:03
 **/
@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    public void insert(User user) {
        userMapper.insert(user);
        Random random = new Random();
        int x = random.nextInt(10);
        if (x < 5) {
            throw new RuntimeException("测试异常");
        }
        Role role = new Role();
        role.setName(user.getName() + ":role");
        roleMapper.insert(role);
    }

    public PageInfo<User> listByPage(int page, int rows) {
        PageHelper.startPage(page, rows);
        List<User> list = userMapper.list();
        return new PageInfo<>(list);
    }
}
